import {Toast} from "vant";
import ajax from './api/zhangkj'
import loading from './loading'

export default function (setting = {type: '', params: {}}) {
  let timeOut = ysf ? 0 : 1000;
  let type = setting.type || '';
  let params = setting.params || {};
  let show = params.show || 1; // 1为打开， 其他参数为隐藏（包括非零元素）
  // todo 客服加载存在延迟，因此hack
  console.warn('我传递的客服参数params:::',params)
  setTimeout(() => {
    switch (type) {
      case 'product':
        ysf('product', {
          show,
          sendByUser: 1, // 是否由访客手动发送，1为是，其他参数为否
          actionText: '发送链接', // 发送按钮文案
          actionTextColor: '#000000', // 发送按钮文案颜色（16进制格式）
          title: params.productName,
          desc: params.productDesc,
          picture: params.goodsIcon,
          note: params.goodsNo,
          success: () => {
            console.log('send product success');
            ysf('getUnreadMsg');
            loading.close();
            ysf('open');
          },
          error: () => {
            console.log('send product error');
            loading.close();
            Toast({message: '打开客服失败，请稍后重试'})
          }
        });
        break;
      case 'online':
        localStorage.getItem('userInfo');
        if (localStorage.getItem('memberId')) {
          ajax.getMemberInfo({}).then(res => {
            ysf('config', {
              uid: 'hdsg_' + localStorage.getItem('memberId'),
              name: res.data.nickName,
              email: '',
              mobile: res.data.phone,
              success: function () {
                loading.close();
                ysf('getUnreadMsg');
                ysf('open');
              },
              error: function () {
                // handle error
                Toast({message: '打开客服失败，请稍后重试'})
              }
            });
          }, err => {
            ysf('getUnreadMsg');
            ysf('open', {
              success() {
                loading.close();
              },
              err() {
                Toast({message: '打开客服失败，请稍后重试'})
              }
            });
          })
        }else{
          ysf('getUnreadMsg');
          ysf('open', {
            success() {
              loading.close();
            },
            err() {
              Toast({message: '打开客服失败，请稍后重试'})
            }
          });
        }
        break;
      default:
        ysf('open', {
          success() {
            loading.close();
          },
          err() {
            Toast({message: '打开客服失败，请稍后重试'})
          }
        });
        break;
    }
  }, timeOut)
}
